<template>
  <div class="spaceManagePage">
    <!-- 标题栏 -->
    <a-flex justify="space-between">
      <h2>空间管理</h2>
      <a-space>
        <a-button type="primary" href="/add_space" target="_blank">+ 创建空间</a-button>
        <a-button type="primary" ghost href="/space_analyze?queryPublic=1" target="_blank"
          >分析公共图库</a-button
        >
        <a-button type="primary" ghost href="/space_analyze?queryAll=1" target="_blank"
          >分析全空间</a-button
        >
      </a-space>
    </a-flex>
    <div style="margin-bottom: 16px"></div>

    <!-- 搜索栏 -->
    <a-form layout="inline" :model="searchParam" @finish="doSearch">
      <a-form-item label="空间名称">
        <a-input v-model:value="searchParam.spaceName" placeholder="请输入空间名称" />
      </a-form-item>
      <a-form-item name="spaceLevel" label="空间级别">
        <a-select
          style="min-width: 180px"
          v-model:value="searchParam.spaceLevel"
          placeholder="请选择空间级别"
          :options="SPACE_LEVEL_OPTIONS"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="空间类别" name="spaceType">
        <a-select
          v-model:value="searchParam.spaceType"
          :options="SPACE_TYPE_OPTIONS"
          placeholder="请输入空间类别"
          style="min-width: 180px"
          allow-clear
        />
      </a-form-item>

      <a-form-item label="用户Id">
        <a-input v-model:value="searchParam.userId" placeholder="请输入用户Id" />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <!-- 表格 -->
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
      :scroll="{ x: 1200 }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'spaceLevel'">
          <div>{{ SPACE_LEVEL_MAP[record.spaceLevel] }}</div>
        </template>
        <!-- 空间类别 -->
        <template v-if="column.dataIndex === 'spaceType'">
          <a-tag>{{ SPACE_TYPE_MAP[record.spaceType] }}</a-tag>
        </template>

        <template v-if="column.dataIndex === 'spaceUserInfo'">
          <div>大小: {{ formatBytes(record.totalSize) }}/{{ formatBytes(record.maxSize) }}</div>
          <div>数量: {{ record.totalCount }}/{{ record.maxCount }}</div>
        </template>

        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.dataIndex === 'editTime'">
          {{ dayjs(record.editTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>

        <template v-else-if="column.key === 'action'">
          <a-space wrap>
            <a-button type="primary" danger @click="handleDelete(record.id)">删除</a-button>
            <a-button type="link" :href="`/space_analyze?spaceId=${record.id}`" target="_blank"
              >分析</a-button
            >
            <a-button type="link" :href="`/add_space?id=${record.id}`" target="_blank"
              >编辑</a-button
            >
          </a-space>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { deleteSpaceUsingPost, listSpaceByPageUsingPost } from '@/api/spaceController'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { SPACE_TYPE_MAP } from '@/constants/space'
import { SPACE_LEVEL_MAP, SPACE_LEVEL_OPTIONS } from '@/constants/space'
import { formatBytes } from '@/utils'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '空间名称',
    dataIndex: 'spaceName',
  },
  {
    title: '空间级别',
    dataIndex: 'spaceLevel',
  },
  {
    title: '空间类别',
    dataIndex: 'spaceType',
  },
  {
    title: '使用情况',
    dataIndex: 'spaceUserInfo',
  },
  {
    title: '用户 id',
    dataIndex: 'userId',
    width: 80,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '编辑时间',
    dataIndex: 'editTime',
  },
  {
    title: '操作',
    key: 'action',
  },

  {
    title: '空间类别',
    dataIndex: 'spaceType',
  },
]

const dataList = ref<API.Space[]>([])
const total = ref(0)
//搜索条件
const searchParam = reactive<API.SpaceQueryRequest>({
  pageNum: 1,
  pageSize: 10,
  sortField: 'createTime',
  sortOrder: 'descend',
})

const pagination = computed(() => {
  return {
    pageNum: searchParam.pageNum,
    pageSize: searchParam.pageSize,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条数据`,
  }
})
async function fetchData() {
  const res = await listSpaceByPageUsingPost({ ...searchParam })
  if (res.data.code == 200 && res.data.data) {
    dataList.value = res.data.data.records ?? []
    console.log(dataList)

    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取数据失败' + res.data.message)
  }
}

//表格变换后,重新获取数据
function doTableChange(pagination: any) {
  searchParam.pageNum = pagination.current
  searchParam.pageSize = pagination.pageSize
  fetchData()
}
//搜索逻辑
function doSearch() {
  searchParam.pageNum = 1
  searchParam.pageSize = 10
  fetchData()
}
//删除逻辑
function handleDelete(id: number) {
  if (!id) {
    return
  }
  deleteSpaceUsingPost({ id })
    .then((res) => {
      if (res.data.code == 200) {
        fetchData()
        message.success('删除成功')
      } else {
        message.success('删除成功')
      }
    })
    .catch((err) => {
      message.error('删除失败' + err.data.message)
    })
}

//页面加载时获取数据
onMounted(() => {
  fetchData()
})
</script>

